package com.bosc.automationui.service;

import com.bosc.automationui.entity.ETLJob;
import com.bosc.automationui.repository.ETLJobRepository;
import com.bosc.automationui.util.StringToDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ETLJobService {

    @Autowired
    ETLJobRepository etlJobRepository;

//    @Autowired
//    JobHistoryRepository jobHistoryRepository;

    public HashMap<String, Object> deleteById(String jobId) {
        HashMap<String, Object> responseJson = new HashMap<>();

//        long jobIdLong = Long.parseLong(jobId);  //此行已不用, 由于库中主键为String
        etlJobRepository.deleteById(jobId);
//        jobHistoryRepository.deleteById(jobId);

        responseJson.put("code", "0");
        responseJson.put("message", "success");

        return responseJson;
    }

    public HashMap<String, Object> updateDate(String jobId, String date) {
        HashMap<String, Object> responseJson = new HashMap<>();

//        long jobIdLong = Long.parseLong(jobId);   //此行已不用, 由于库中主键为String
        Date formatDate = StringToDateUtil.strToDate(date);
        ETLJob etlJob = etlJobRepository.findById(jobId).get();
        etlJobRepository.deleteById(jobId);

        System.out.println(formatDate.toString());
        System.out.println(etlJob.toString());

        etlJob.setLastTxDate(formatDate);
        etlJob.setJobType("D");
        etlJob.setLastJobStatus("Done");
        System.out.println(etlJob.toString());
        etlJobRepository.save(etlJob);

        responseJson.put("code", "0");
        responseJson.put("message", "success");

        return responseJson;
    }

    public HashMap<String, Object> setVoid(String jobId) {
        HashMap<String, Object> responseJson = new HashMap<>();
//        long jobIdLong = Long.parseLong(jobId);   //此行已不用, 由于库中主键为String

        ETLJob etlJob = etlJobRepository.findById(jobId).get();
        etlJob.setJobType("V");
        etlJobRepository.save(etlJob);

        responseJson.put("code", "0");
        responseJson.put("message", "success");

        return responseJson;
    }

    public HashMap<String, Object> getList() {
        HashMap<String, Object> responseJson = new HashMap<>();
        List<ETLJob> responseList = new ArrayList<>();
        Iterable<ETLJob> etlJobStatusList = etlJobRepository.findAllByOrderByLastTxDateDesc();

        responseJson.put("code", "0");
        responseJson.put("msg", "success");

        for (ETLJob jobStatus : etlJobStatusList) {
            responseList.add(jobStatus);
        }

        responseJson.put("data", responseList);

        return responseJson;
    }

    public HashMap<String, Object> downloadLog(String jobId) {
        HashMap<String, Object> responseJson = new HashMap<>();
        long jobIdLong = Long.parseLong(jobId);

        String logPath = "/" + jobIdLong;

        responseJson.put("code", "0");
        responseJson.put("message", "success");
        responseJson.put("downloadPath", logPath);

        System.out.println("Download log for jobid: " + jobId);
        return responseJson;
    }

    public HashMap<String, Object> search(String col, String condition) {
        HashMap<String, Object> responseJson = new HashMap<>();
        responseJson.put("code", "0");
        responseJson.put("message", "success");
        List<ETLJob> resutlList = new ArrayList<>();
        switch (col) {
            case "etlJob":
                resutlList = etlJobRepository.findAllByEtlJob(condition);
                break;
            case "etlSystem":
                resutlList = etlJobRepository.findAllByEtlSystem(condition);
                break;
            case "lastTxDate":
                Date searchDate = StringToDateUtil.strToDate(condition);
                resutlList = etlJobRepository.findAllByLastTxDate(searchDate);
                break;
        }
        responseJson.put("data", resutlList);
        return responseJson;
    }
}
